@extends('layouts._app')
@section('content')
    <body>
    <div class="wrap-container">
        <div class="column-content-detail">
            {!! Form::open(['route' => 'blog.feedback.index','class'=>'layui-form','method'=>'get']) !!}
            <div class="layui-form-item">
                <div class="layui-inline tool-btn">
                    <a class="layui-btn layui-btn-small layui-btn-primary" id='email-refresh-btn'
                       href="{{route('blog.feedback.index')}}" data-title='刷新'>
                        <i class="layui-icon">&#x1002;</i>
                    </a>
                    <button class="layui-btn layui-btn-small layui-btn-danger delBtn hidden-xs">
                        <i class="layui-icon">&#xe640;</i>
                    </button>
                </div>
                <div class="layui-inline">
                    {!! Form::search('content',"{$request['content']}",['class'=>'layui-input','placeholder'=>'请输入内容']) !!}
                </div>
                <div class="layui-inline">
                    <select name="category" lay-filter="" lay-search>
                        <option value="-1">全部</option>
                        @foreach($category as  $value)
                            <option value="{{$value['id']}}"
                                    @if($request['category'] == $value['id']) selected @endif>{{$value['name']}}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="layui-inline">
                    <select name="static" lay-filter="" lay-search>
                        <option value="-1">全部</option>
                        @foreach(config('articleFeedback') as $key => $value)
                            <option value="{{$key}}" @if($request['static'] == $key) selected @endif>{{$value['title']}}
                            </option>
                        @endforeach
                    </select>
                </div>
                {!! Form::submit('搜索',['class'=>'layui-btn layui-btn-normal','lay-submit'=>'feedback']) !!}
                <div class="layui-inline" style="float: right;">
                    <select id="change_category" lay-filter="category" lay-search data-id="feedback">
                        <option selected>下载</option>
                        <option>.txt</option>
                        <option>.xlsx</option>
                    </select>
                </div>
            </div>
            {!! Form::close() !!}
            @include('layouts._flash')
            <div class="layui-form" id="table-list">
                <table class="layui-table">
                    <thead>
                    <tr>
                        <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
                        <th>用户ID</th>
                        <th>用户名</th>
                        <th>分类</th>
                        <th>二级分类</th>
                        <th>联系方式</th>
                        <th>图片</th>
                        <th>内容</th>
                        <th>出网ip</th>
                        <th>出网国家</th>
                        <th>出网地区</th>
                        <th>出网省</th>
                        <th>出网市</th>
                        <th>出网县</th>
                        <th>处理回复消息</th>
                        <th>时间</th>
                        <th>当前状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($feedback as $value)
                        <tr data-id="{{$value->id}}">
                            <td><input type="checkbox" name="del" lay-skin="primary" data-id="{{$value->id}}"></td>
                            <td title="@if(!$value->user) 未知 @else {{$value->user->name}}@endif">@if(!$value->user)
                                    未知 @else {{$value->user_id}}@endif</td>
                            <td title="@if(!$value->user) 未知 @else {{$value->user->name}}@endif">@if(!$value->user)
                                    未知 @else {{$value->user->name}}@endif</td>
                            <td title="{{$value->category->name ?? '无'}}">{{sub($value->category->name ?? '无',20)}}</td>
                            <td title="{{$value->parent->name ?? '无'}}">{{sub($value->parent->name ?? '无',20)}}</td>
                            <td title="{{$value->phone}}">{{sub($value->phone,20)}}</td>
                            <td title="{{$value->content}}" class="layer-photos-demo">@if(!$value->pic->isEmpty())
                                    @foreach($value->pic as $pic)
                                        <img style="height: 60px;"
                                             src="{{ assetImage($pic->url)}}"
                                             alt="{{$pic->filename}}">
                                    @endforeach @endif</td>
                            <td title="{{$value->content}}">{{sub($value->content,20)}}</td>
                            <td title="{{$value->ip}}">{{sub($value->ip,20)}}</td>
                            <td title="{{$value->country}}">{{sub($value->country,20)}}</td>
                            <td title="{{$value->area}}">{{sub($value->area,20)}}</td>
                            <td title="{{$value->region}}">{{sub($value->region,20)}}</td>
                            <td title="{{$value->city}}">{{sub($value->city,20)}}</td>
                            <td title="{{$value->county}}">{{sub($value->county,20)}}</td>
                            <td title="{{$value->fail}}">{{sub($value->fail,20)}}</td>
                            <td title="{{$value->updated_at}}">{{sub($value->updated_at,20)}}</td>
                            <td title="{{config('articleFeedback')[$value->static]['title']}}">
                                <a class="layui-btn layui-btn-mini layui-btn-{{config('articleFeedback')[$value->static]['color']}}"
                                   href="javascript:void(0);"
                                   onclick="statusFail('{{$value->static}}','{{$value->fail}}','{{$value->id}}')"
                                   data-id="{{$value->static}}">{{config('articleFeedback')[$value->static]['title']}}</a>
                            </td>
                            <td>
                                <div class="layui-inline">
                                    <a class="layui-btn layui-btn-small layui-btn-danger"
                                       href="{{route('blog.feedback.destroy',$value->id)}}"
                                       data-method="delete" title="意见反馈删除"
                                       data-token="{{csrf_token()}}" data-confirm="确认删除吗?">
                                        <i class="layui-icon">&#xe640;</i></a>
                                </div>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
                <!--tp分页-->
                <div class="page-wrap">
                    <p style="float: left;">注 : 共 {{$feedback->total()}} 条记录</p>
                    {{ $feedback->links() }}
                </div>
            </div>
        </div>
    </div>
    <div style="display: none;" id="changeArea">
        <form class="layui-form" action="">
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">处理状态</label>
                <div class="layui-input-inline">
                    <select lay-search id="status" class="layui-form-select">
                        <option value="-1">全部</option>
                        @foreach(config('articleFeedback') as $key => $value)
                            <option value="{{$key}}">{{$value['title']}}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">处理说明</label>
                <div class="layui-input-inline">
                    {!!  Form::textarea('content','请输入内容！！！',['class'=>'layui-textarea','id'=>'content']) !!}
                </div>
            </div>
        </form>
    </div>
    <script src="{{asset('static/admin/layui/layui.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('static/admin/js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/jquery.min.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/destroy.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script>
        function statusFail(status, fail, id) {
            layui.use(['form'], function () {
                var form = layui.form();
                $('#status').val(status);
                $('#content').val(fail);
                form.render();
                layer.open({
                    type: 1,
                    btnAlign: 'c',
                    area: ['600px', '400px'],
                    title: '处理',
                    content: $("#changeArea"),
                    btn: ['提交', '取消'],
                    btn1: function () {
                        var title = $('#content').val();
                        var status = $('#status').val();
                        $.ajax({
                            type: 'PATCH',
                            url: '{{route('blog.feedback.status')}}',
                            data: {status: status, id: id, fail: title},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                })
            });
        }

        layui.use(['jquery', 'dialog'], function () {
            var $ = layui.jquery;
            var dialog = layui.dialog;
            layer.ready(function () { //为了layer.ext.js加载完毕再执行
                layer.photos({
                    photos: '.layer-photos-demo'
                    , shift: 5 //0-6的选择，指定弹出图片动画类型，默认随机
                });
            });
            //顶部批量删除
            $('.delBtn').click(function () {
                var ids = [];
                $("input[name='del']:checked").each(function (i) {
                    ids[i] = $(this).data('id');
                });
                if (ids.length == 0) {
                    layer.msg('请选择要删除的', {icon: 5, time: 3000});
                    return false;
                }
                dialog.confirm({
                    message: '您确定要删除选中项',
                    success: function () {
                        $.ajax({
                            type: 'POST',
                            url: '{{route('blog.feedback.delete')}}',
                            data: {id: ids, filter: true},
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                    cancel: function () {
                        return false;
                    }
                })
                return false;

            }).mouseenter(function () {

                dialog.tips('批量删除', '.delBtn');

            })


            layui.use(['form'], function () {
                var form = layui.form();

                form.on('select(category)', function (e) {
                    if (e.value == '下载') {
                        return false;
                    }
                    /**
                     * 写入文件下载
                     * @param id
                     */
                    var table = $("#change_category").data('id');
                    var ext = e.value;
                    dialog.confirm({
                        message: '这可能会耗费很长时间！是否继续',
                        success: function () {
                            window.open('{{route('TextFile')}}' + '?table=' + table + '&ext=' + ext, '_parent');
                            window.location.reload();
                        },
                    });
                });
                form.render();
            });

        })
    </script>
    </body>
@endsection
